﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using VanTaiBLL;
using VanTaiProperty;

namespace VanTaiTMS.SubPage
{
    public partial class Uc_Mien : System.Web.UI.UserControl
    {
        private DataTable _dtMien;
        private readonly clsTMRegionBusiness _clsMien = new clsTMRegionBusiness();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridDropQuocGia();
        }
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {
            BindGridDropQuocGia();
        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTenMien.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên miền !";
                OpenDialog("#ucthongbaoMien");
                updatePanelThongBao.Update();
            }
            if (txtMaMien.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã miền !";
                OpenDialog("#ucthongbaoMien");
                updatePanelThongBao.Update();
            }
            if (drQuocGia.SelectedValue == "-1")
            {
                lbThongBaoLoi.Text = "Vui lòng chọn quốc gia !";
                OpenDialog("#ucthongbaoMien");
                updatePanelThongBao.Update();
            }
            if (txtMaMien.Text != "" && txtTenMien.Text != "" && drQuocGia.SelectedValue != "-1")
            {
                InsertMien();
            }
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaoMien");
        }
        protected void LinkThemNhanhQuocGia_Click(object sender, EventArgs e)
        {
            OpenDialog("#ThemNhanhQuocGia");
        }
        protected void BindGridDropQuocGia()
        {
            var clsQuocGia = new clsTMNationalBusiness();
            DataTable dtQuocgia = clsQuocGia.Search("");
            if (dtQuocgia != null && dtQuocgia.Rows.Count > 0)
            {
                drQuocGia.DataSource = dtQuocgia;
                drQuocGia.DataValueField = "STT";
                drQuocGia.DataTextField = "TenQuocGia";
                drQuocGia.DataBind();
            }
        }

        //Insert
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            _dtMien = _clsMien.BindGirdTMRegion("");
            if (_dtMien != null && _dtMien.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtMien.Rows)
                {
                    if (txtMaMien.Text == dr["MAMien"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertnName()
        {
            bool kiemtra = false;
            _dtMien = _clsMien.BindGirdTMRegion("");
            if (_dtMien != null && _dtMien.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtMien.Rows)
                {
                    if (txtTenMien.Text == dr["TENMien"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertMien()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã quốc gia này đã tồn tại !";
                    OpenDialog("#ucthongbaoMien");
                    updatePanelThongBao.Update();
                }
                else
                {
                    if (CheckInsertnName())
                    {
                        lbThongBaoLoi.Text = "Tên quốc gia này đã tồn tại !";
                        OpenDialog("#ucthongbaoMien");
                        updatePanelThongBao.Update();
                    }
                    else
                    {
                        var obj = new clsTMRegionProperty
                        {
                            Region_Alias = txtMaMien.Text,
                            Region_Name = txtTenMien.Text,
                            Region_NameC = txtTenMienC.Text,
                            Region_NameE = txtTenMienE.Text,
                            National_ID = float.Parse(drQuocGia.SelectedValue),
                            Region_Note = txtGhiChu.Text
                        };
                        clsTMRegionBusiness.InsertTMRegion(obj);
                        lbThongBaoLoi.Text = "Thêm mới thành công mẫu tin !";
                        OpenDialog("#ucthongbaoMien");
                        updatePanelThongBao.Update();
                        ClearTextbox();
                        txtMaMien.Text = "MM000" + MaTuDong();
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                updatePanelThongBao.Update();
            }
        }

        #region[MaTangTuDong]

        protected int MaTuDong()
        {
            int i = 0;
            _dtMien = _clsMien.BindGirdTMRegion("");
            if (_dtMien != null && _dtMien.Rows.Count > 0)
            {
                int max = Convert.ToInt32(_dtMien.Rows[0]["STT"].ToString());
                for (i = 0; i < _dtMien.Rows.Count; i++)
                {
                    if (int.Parse(_dtMien.Rows[i]["STT"].ToString()) > max)
                    {
                        max = Convert.ToInt32(_dtMien.Rows[i]["STT"].ToString());
                    }
                }
                i = max + 1;
            }
            return i;
        }

        #endregion

        #region[ClearTextBox]

        protected void ClearTextbox()
        {
            txtGhiChu.Text = "";
            txtTenMienE.Text = "";
            txtTenMienC.Text = "";
            txtTenMien.Text = "";
            txtMaMien.Text = "";
            drQuocGia.SelectedIndex = -1;
        }

        #endregion

        #region[OpenDialog]

        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }

        private void CloseDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }

        #endregion
    }
}